package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.dao.PaasFormPaasSubDataDao;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasFormSubDataEsModel;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.saas.constant.SubFormTableNameConstant;
import com.xbongbong.saas.domain.entity.CostAdjustSubDataEntity;
import com.xbongbong.saas.model.CostAdjustSubDataModel;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author WuHB
 * @date 2021/11/22 09:12
 */
@Service("costAdjustSubDataModel")
public class CostAdjustSubDataModelImpl implements CostAdjustSubDataModel {

    @Resource
    private PaasFormPaasSubDataDao paasFormPaasSubDataDao;
    @Resource
    private PaasFormSubDataEsModel paasFormSubDataEsModel;

    /**
     * 插入
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer insert(CostAdjustSubDataEntity entity) throws XbbException {
        int insert = paasFormPaasSubDataDao.insert(entity, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        paasFormSubDataEsModel.insert(entity, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB);
        return insert;
    }

    /**
     * 更新
     *
     * @param entity 实体
     * @return 是否成功
     * @throws XbbException 数据库操作异常
     */
    @Override
    public Integer update(CostAdjustSubDataEntity entity) throws XbbException {
        int update = paasFormPaasSubDataDao.update(entity, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        paasFormSubDataEsModel.update(entity, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB);
        return update;
    }

    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return 符合查询条件的记录
     */
    @Override
    public List<CostAdjustSubDataEntity> findEntitys(Map<String, Object> param) {
        return (List<CostAdjustSubDataEntity>) paasFormPaasSubDataDao.findEntitys(param, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
    }

    /**
     * 获取符合查询条件的记录总数
     *
     * @param param 查询条件
     * @return 符合查询条件的记录总数
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return paasFormPaasSubDataDao.getEntitysCount(param, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
    }

    /**
     * Description: 查询符合条件的数据（无子表单数据）
     *
     * @param param 查询条件
     * @return java.util.List<T>
     */
    @Override
    public List<CostAdjustSubDataEntity> findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 批量添加
     *
     * @param entities 调整单子表单实体类集合
     * @return 影响条数
     * @throws XbbException 异常
     */
    @Override
    public Integer insertBatch(List<CostAdjustSubDataEntity> entities) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.insertBatch(entities, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        paasFormSubDataEsModel.insertBatch(entities, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB);
        return integer;
    }

    /**
     * 根据dataId查询成本调整单子表单
     *
     * @param dataId 数据ID
     * @param corpid 公司ID
     * @return 成本调整单子表单集合
     */
    @Override
    public List<CostAdjustSubDataEntity> getByDataId(Long dataId, String corpid) {
        return (List<CostAdjustSubDataEntity>) paasFormPaasSubDataDao.getByDataId(dataId, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
    }

    /**
     * 根据dataId集合查询成本调整单子表单
     *
     * @param dataIds 数据ID集合
     * @param corpid  公司ID
     * @param delEnum 是否删除 @see {@link DelEnum}
     * @return
     */
    @Override
    public List<CostAdjustSubDataEntity> getByDataIds(List<Long> dataIds, String corpid, DelEnum delEnum) {
        return (List<CostAdjustSubDataEntity>) paasFormPaasSubDataDao.getByDataIds(dataIds, corpid, delEnum.getDel(), SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
    }

    /**
     * 批量修改成本调整单子表单
     *
     * @param updateDataList 修改数据集合
     * @param corpid         公司ID
     * @return 影响条数
     * @throws XbbException 异常
     */
    @Override
    public Integer updateBatch(List<UpdateDataEntity> updateDataList, String corpid) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.updateBatch(updateDataList, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        if (!Objects.equals(0, integer)) {
            paasFormPaasSubDataDao.updateBatchTime(updateDataList, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
            paasFormSubDataEsModel.updateBatch(updateDataList, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB, WriteRequest.RefreshPolicy.NONE);
        }
        return integer;
    }

    /**
     * 批量删除成本调整单子表单
     *
     * @param corpid        公司ID
     * @param entities      调整单子表单实体类集合
     * @param refreshPolicy WriteRequest.RefreshPolicy
     * @return 影响条数
     * @throws XbbException 异常
     */
    @Override
    public Integer deleteBatch(String corpid, List<CostAdjustSubDataEntity> entities, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        List<Long> ids = new ArrayList<>();
        for (PaasFormSubDataEntity item : entities) {
            ids.add(item.getId());
        }
        Integer result = paasFormPaasSubDataDao.deleteById(ids, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB, refreshPolicy, DelEnum.DELETE);
        return result;
    }

    /**
     * 根据dataId删除成本调整单子表单
     *
     * @param dataId 数据ID
     * @param corpid 公司ID
     * @return 影响条数
     * @throws XbbException 异常
     */
    @Override
    public Integer deleteByDataId(Long dataId, String corpid) throws XbbException {
        List<CostAdjustSubDataEntity> entities = getByDataId(dataId, corpid);
        Integer result = paasFormPaasSubDataDao.deleteByDataId(dataId, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
        return result;
    }

    /**
     * 根据dataId集合删除成本调整单子表单
     *
     * @param dataIds 数据ID集合
     * @param corpid  公司ID
     * @param delEnum 是否删除 @see {@link DelEnum}
     * @return 影响条数
     * @throws XbbException 异常
     */
    @Override
    public Integer deleteBatchByDataId(List<Long> dataIds, String corpid, DelEnum delEnum) throws XbbException {
        List<CostAdjustSubDataEntity> entities = getByDataIds(dataIds, corpid, DelEnum.NORMAL);
        Integer integer = paasFormPaasSubDataDao.deleteBatchByDataId(dataIds, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA, delEnum.getDel());
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB, WriteRequest.RefreshPolicy.NONE, DelEnum.DELETE_SUBFORM);
        return integer;
    }

    /**
     * 根据dataids批量恢复成本调整单子表单
     *
     * @param dataIds 数据ID集合
     * @param corpid  公司ID
     * @throws XbbException 异常
     */
    @Override
    public void restoreBatch(List<Long> dataIds, String corpid) throws XbbException {
        List<CostAdjustSubDataEntity> entities = getByDataIds(dataIds, corpid, DelEnum.DELETE_SUBFORM);
        paasFormPaasSubDataDao.restoreBatch(dataIds, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        paasFormSubDataEsModel.deleteBatch(corpid, entities, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB, WriteRequest.RefreshPolicy.NONE, DelEnum.NORMAL);
    }

    /**
     * 修改删除
     *
     * @param updateDelList 调整单子表单实体类集合
     * @param corpid        公司ID
     * @param refreshPolicy WriteRequest.RefreshPolicy
     * @param delEnum       是否删除 @see {@link DelEnum}
     * @return 影响条数
     * @throws XbbException 异常
     */
    @Override
    public Integer updateDel(List<? extends PaasFormSubDataEntity> updateDelList, String corpid, WriteRequest.RefreshPolicy refreshPolicy, DelEnum delEnum) throws XbbException {
        if (CollectionUtils.isEmpty(updateDelList)) {
            return 0;
        }
        List<Long> ids = new ArrayList<>();
        for (PaasFormSubDataEntity v : updateDelList) {
            ids.add(v.getId());
        }
        Integer result = paasFormPaasSubDataDao.updateDelByIdList(ids, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA, delEnum.getDel());
        paasFormSubDataEsModel.deleteBatch(corpid, updateDelList, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB, refreshPolicy, delEnum);
        return result;
    }

    /**
     * 根据实体批量修改
     *
     * @param list   调整单子表单实体类集合
     * @param corpid 公司ID
     * @return 影响条数
     * @throws XbbException 异常
     */
    @Override
    public Integer updateBatchByEntity(List<? extends PaasFormSubDataEntity> list, String corpid) throws XbbException {
        Integer integer = paasFormPaasSubDataDao.updateBatchByEntity(list, corpid, SubFormTableNameConstant.TB_SAAS_COST_ADJUST_SUB_DATA);
        paasFormSubDataEsModel.updateBatchByEntity(list, IndexTypeEnum.IDX_SAAS_COST_ADJUST_SUB, WriteRequest.RefreshPolicy.NONE);
        return integer;
    }
}
